package com.mbc.web.common.server.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpSession;

import net.sf.jasperreports.engine.JasperPrint;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mbc.common.CommonSingleton;
import com.mbc.common.DataXML;
import com.mbc.web.common.shared.HttpStatus;
import com.mbc.common.dao.DBManagerFactory;
import com.mbc.common.data.CurrencyReferenceData;
import com.mbc.common.data.UserData;
import com.mbc.common.entities.Branch;
import com.mbc.common.entities.BranchRpt;
import com.mbc.common.entities.BranchType;
import com.mbc.common.entities.BranchTypeRpt;
import com.mbc.common.entities.Country;
import com.mbc.common.entities.CountryRpt;
import com.mbc.common.entities.Currency;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.CurrencyRateDetail;
import com.mbc.common.entities.CurrencyRateRpt;
import com.mbc.common.entities.CurrencyRpt;
import com.mbc.common.entities.SystemProperty;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.ForeignConflictException;
import com.mbc.common.exception.LockedUserException;
import com.mbc.common.exception.UnauthorizedAccessException;
import com.mbc.common.manager.BranchTypeMgr;
import com.mbc.common.manager.CountryMgr;
import com.mbc.common.manager.CurrencyMgr;
import com.mbc.common.manager.BranchMgr;
import com.mbc.common.manager.CurrencyRateMgr;
import com.mbc.common.manager.SystemMgr;
import com.mbc.common.manager.UserMgr;
import com.mbc.web.common.client.controller.WebCommonContoller;
import com.mbc.web.common.server.ServerSingleton;
import com.mbc.web.common.server.SessionConstants;
import com.mbc.web.common.server.util.FileUtil;
import com.mbc.web.common.shared.dto.AppItemListDTO;
import com.mbc.web.common.shared.dto.BranchDataListDTO;
import com.mbc.web.common.shared.dto.BranchListDTO;
import com.mbc.web.common.shared.dto.BranchTypeDataListDTO;
import com.mbc.web.common.shared.dto.BranchViewLoadDTO;
import com.mbc.web.common.shared.dto.CountryDataListDTO;
import com.mbc.web.common.shared.dto.CurrencyDataListDTO;
import com.mbc.web.common.shared.dto.CurrencyRateDataListDTO;
import com.mbc.web.common.shared.dto.CurrencyRateViewLoadDTO;

public class WebCommonContollerImpl extends RemoteServiceServlet implements
		WebCommonContoller {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4980979325593279313L;

	private HttpSession getSession()
	{
		return getThreadLocalRequest().getSession(); 
	}
	
	@Override
	public AppItemListDTO getAllAppItems(){
		AppItemListDTO l_wrapper = new AppItemListDTO();
		try {
			SystemMgr l_SysMgr = new SystemMgr(getSession().getId());
			l_wrapper.setAppItemList(l_SysMgr.getAllAppItem());
		} catch (Exception e) {
			e.printStackTrace();
			l_wrapper.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_wrapper;
	}

	@Override
	public UserData login(String loginID, String password, String dsName)
			throws IllegalArgumentException {
			UserData l_UserData= new UserData();
		try {	
			DataXML l_DataXml = new DataXML();
			Properties l_properties = l_DataXml.getDSProperties(ServerSingleton.ABSOLUTE_PATH, dsName);
			HttpSession l_Session = getSession();
			DBManagerFactory.addProperties(l_Session.getId(), l_properties);
			DBManagerFactory.addDataSource(l_Session.getId(), dsName);
			
			SystemMgr l_SystemMgr = new SystemMgr(l_Session.getId());
//			BranchMgr l_branchMgr = new BranchMgr(l_Session.getId());
			
			l_UserData = l_SystemMgr.login(loginID, password);
			
			l_UserData.setMyanmarNumberValues(readNumberValues());
			l_UserData.setMyanmarNumbers(readNumbers());
			String[] l_curName = readCurrencyName();
			l_UserData.setMyanmarCurrencyName(l_curName[0]);
			l_UserData.setMyanmarDecName(l_curName[1]);
			
			l_Session.setAttribute(SessionConstants.LONGIN_ID, l_UserData.getLoginID());
			l_Session.setAttribute(SessionConstants.BRANCH_CODE, l_UserData.getBranchCode());
			l_Session.setAttribute(SessionConstants.BRANCH_ID, l_UserData.getBranchId());
			l_Session.setAttribute(SessionConstants.USER_ID, l_UserData.getUserID());
			l_Session.setAttribute(SessionConstants.COUNTRY_CODE, l_UserData.getCountryCode());
			
			l_UserData.setReponseStatus(HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof UnauthorizedAccessException)
				l_UserData.setReponseStatus(HttpStatus.UNAUTHORIZED);
			else if(e instanceof LockedUserException)
				l_UserData.setReponseStatus(HttpStatus.getStatusExtension(HttpStatus.UNAUTHORIZED, 1));
			else
				l_UserData.setReponseStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_UserData;
	}

	@Override
	public List<String[]> getDSNames() throws IllegalArgumentException {
		try {
			ServerSingleton.ABSOLUTE_PATH = getServletContext().getRealPath("") + "\\";
			CommonSingleton.ABSOLUTE_PATH = ServerSingleton.ABSOLUTE_PATH;
			
			DataXML l_DataXml = new DataXML();
			return l_DataXml.getDSNames(ServerSingleton.ABSOLUTE_PATH + "Data\\data-sources\\data-sources.xml");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return new ArrayList<String[]>();
	}

	@Override
	public BranchListDTO getAllBranch() {
		BranchListDTO l_dto = new BranchListDTO();
		try {
			BranchMgr l_BranchMgr = new BranchMgr(getSession().getId());
			l_dto.setBranchList(l_BranchMgr.getAllBranch());
		} catch (Exception e) {
			e.printStackTrace();
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public int createCurrency(Currency currency) {
		try {
			CurrencyMgr l_CurrencyMgr = new CurrencyMgr(getSession().getId());
			currency.setUserid(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			l_CurrencyMgr.createCurrency(currency);
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof DataDuplicationException)
				return HttpStatus.BAD_REQUEST;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int updateCurrency(Currency currency) {
		try {
			CurrencyMgr l_CurrencyMgr = new CurrencyMgr(getSession().getId());
			currency.setUserid(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			l_CurrencyMgr.updateCurrency(currency);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int deleteCurrency(long currencyId) {
		try {
			CurrencyMgr l_CurrencyMgr = new CurrencyMgr(getSession().getId());
			l_CurrencyMgr.deleteCurrency(currencyId,Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof ForeignConflictException)
				return HttpStatus.FORBIDDEN;
			else
				return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public CurrencyDataListDTO getCurrencyList(Currency cri) {
		CurrencyDataListDTO l_dto = new CurrencyDataListDTO();
		try {
			CurrencyMgr l_CurrencyMgr = new CurrencyMgr(getSession().getId());
			l_dto.setCurrencyDataList(l_CurrencyMgr.getCurrencyList(cri));
		} catch (Exception e) {
			e.printStackTrace();
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}
	
	/** MBC **/
	public Currency findCurrencyByCode(String code) {
		try {
			CurrencyMgr l_CurrencyMgr = new CurrencyMgr(getSession().getId());
			return l_CurrencyMgr.findCurrencyByCode(code);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public int createBranch(Branch branch) {
		try {
			BranchMgr l_BranchMgr = new BranchMgr(getSession().getId());
			branch.setCreatedBy(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			branch.setCreatedDate(new Date());
			branch.setModifiedBy(branch.getCreatedBy());
			branch.setModifiedDate(branch.getCreatedDate());
			l_BranchMgr.createBranch(branch);
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof DataDuplicationException)
				return HttpStatus.BAD_REQUEST;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int updateBranch(Branch branch) {
		try {
			BranchMgr l_BranchMgr = new BranchMgr(getSession().getId());
			branch.setModifiedBy(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			branch.setModifiedDate(new Date());
			l_BranchMgr.updateBranch(branch);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int deleteBranch(long key) {
		try {
			BranchMgr l_BranchMgr = new BranchMgr(getSession().getId());
			l_BranchMgr.deleteBranch(key,Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof ForeignConflictException)
				return HttpStatus.FORBIDDEN;
			else
				return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public BranchDataListDTO findBranches(Branch cri) {
		BranchDataListDTO l_dto = new BranchDataListDTO();
		try {
			if(cri.getStatus().equals("true"))
				cri.setBranchId(Long.parseLong(getSession().getAttribute(SessionConstants.BRANCH_ID).toString()));
			BranchMgr l_BranchMgr = new BranchMgr(getSession().getId());
			l_dto.setBranchDataList(l_BranchMgr.findBranch(cri));
		} catch (Exception e) {
			e.printStackTrace();
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}
	
	/** MBC **/
	public Branch findBranchByCode(String code) {
		try {
			BranchMgr l_BranchMgr = new BranchMgr(getSession().getId());
			return l_BranchMgr.findBranchByCode(code);
		} catch (Exception e) {
			return null;
		}
	}


	@Override
	public BranchViewLoadDTO getBranchViewLoadDTO() {
		BranchViewLoadDTO l_dto = new BranchViewLoadDTO();
		try {
			CountryMgr l_CountryMgr = new CountryMgr(getSession().getId());
			BranchTypeMgr l_BranchTypeMgr = new BranchTypeMgr(getSession().getId());
			
			l_dto.setBranchTypeList(l_BranchTypeMgr.findAll());
			l_dto.setCountryList(l_CountryMgr.findAll());
		} catch (Exception e) {
			e.printStackTrace();
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public int createCurrencyRate(CurrencyRate currencyRate) {
		try {
			CurrencyRateMgr l_CurrencyRateMgr = new CurrencyRateMgr(getSession().getId());
			currencyRate.setBranchId(Long.parseLong(getSession().getAttribute(SessionConstants.BRANCH_ID).toString()));
			currencyRate.setUserid(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			l_CurrencyRateMgr.createCurrencyRate(currencyRate);
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof DataDuplicationException)
				return HttpStatus.BAD_REQUEST;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int updateCurrencyRate(CurrencyRate currencyRate) {
		try {
			CurrencyRateMgr l_CurrencyRateMgr = new CurrencyRateMgr(getSession().getId());
			currencyRate.setUserid(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
			l_CurrencyRateMgr.updateCurrencyRate(currencyRate);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int deleteCurrencyRate(long key) {
		try {
			CurrencyRateMgr l_CurrencyRateMgr = new CurrencyRateMgr(getSession().getId());
			l_CurrencyRateMgr.deleteCurrencyRate(key,Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof ForeignConflictException)
				return HttpStatus.FORBIDDEN;
			else
				return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public CurrencyRate findCurrencyRate(long key) {
		try {
			CurrencyRateMgr l_CurrencyRateMgr = new CurrencyRateMgr(getSession().getId());
			return l_CurrencyRateMgr.findCurrencyRate(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public CurrencyRateDataListDTO findCurrencyRates(CurrencyRate params) {
		CurrencyRateDataListDTO l_dto = new CurrencyRateDataListDTO();
		try {
			CurrencyRateMgr l_CurrencyRateMgr = new CurrencyRateMgr(getSession().getId());
			params.setBranchId(Long.parseLong(getSession().getAttribute(SessionConstants.BRANCH_ID).toString()));
			l_dto.setCurrencyRateDataList(l_CurrencyRateMgr.getList(params));
		} catch (Exception e) {
			e.printStackTrace();
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}
	
	/** MBC **/
	public CurrencyRate findCurrencyRateByCode(String code) {
		try {
			CurrencyRateMgr l_CurrencyRateMgr = new CurrencyRateMgr(getSession().getId());
			return l_CurrencyRateMgr.findCurrencyRateByCode(code);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public CurrencyRateDetail findLatestDetail(long currencyRate) {
		return null;
	}

	@Override
	public CurrencyRateViewLoadDTO getCurrencyRateViewLoadDTO() {
		CurrencyRateViewLoadDTO l_dto = new CurrencyRateViewLoadDTO();
		try {
			CurrencyMgr l_CurrencyMgr = new CurrencyMgr(getSession().getId());
			l_dto.setCurrencyList(l_CurrencyMgr.getAll());
		} catch (Exception e) {
			e.printStackTrace();
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public CurrencyRateDetail findLatestDetail(long sourceCurrencyId,
			long convertCurrencyId) {
		try {
			CurrencyRateMgr l_mgr = new CurrencyRateMgr(getSession().getId());
			long branchId = Long.parseLong(getSession().getAttribute(SessionConstants.BRANCH_ID).toString());
			return l_mgr.findLatestCurrencyRateDetail(sourceCurrencyId, convertCurrencyId,branchId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<CurrencyReferenceData> getAllCurrency() {
		try {
			CurrencyMgr l_CurrencyMgr = new  CurrencyMgr(getSession().getId());
			return l_CurrencyMgr.getCurrencyRefList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int viewCurrencyReport(String fromCode, String toCode) {
		try {
			HttpSession l_session = getSession();
			CurrencyMgr l_CurrencyMgr = new CurrencyMgr(l_session.getId());
			JasperPrint l_jrPrint = l_CurrencyMgr.preapreCurrencyReport(fromCode, toCode);
			if(l_jrPrint.getPages().isEmpty())
				return HttpStatus.NOT_FOUND;
			l_session.setAttribute(SessionConstants.REPORT_DATA, l_jrPrint);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int logout() {
		try {
			UserMgr l_UserMgr = new UserMgr(getSession().getId());
			l_UserMgr.updateUserOnlineStatus(Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()), false);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}
	
	private static String[] readNumberValues() throws Exception{
		try {
			ArrayList<String> l_StrList = FileUtil.readFile(ServerSingleton.ABSOLUTE_PATH + "data\\number_values.txt", "UTF-8");
			String[] l_StrArr = new String[l_StrList.size()];
			l_StrArr = l_StrList.toArray(l_StrArr);
			return l_StrArr;
		} catch (Exception e) {
			throw e;
		}
	}
	
	private static String[] readCurrencyName() throws Exception{
		try {
			ArrayList<String> l_StrList = FileUtil.readFile(ServerSingleton.ABSOLUTE_PATH + "data\\currency_name.txt", "UTF-8");
			String[] l_StrArr = new String[l_StrList.size()];
			l_StrArr = l_StrList.toArray(l_StrArr);
			return l_StrArr;
		} catch (Exception e) {
			throw e;
		}
	}
	
	private static String[] readNumbers() throws Exception{
		try {
			ArrayList<String> l_StrList = FileUtil.readFile(ServerSingleton.ABSOLUTE_PATH + "data\\numbers.txt", "UTF-8");
			String[] l_StrArr = new String[l_StrList.size()];
			l_StrArr = l_StrList.toArray(l_StrArr);
			return l_StrArr;
		} catch (Exception e) {
			throw e;
		}
	}
	
	@Override
	public int prepareCurrencyRateReport(CurrencyRateRpt info) {
		try {
				HttpSession l_session = getSession();
				CurrencyRateMgr l_reportmgr = new CurrencyRateMgr(getSession().getId());
				info.setBranchId(Long.parseLong(getSession().getAttribute(
						SessionConstants.BRANCH_ID).toString()));
				JasperPrint l_jrPrint = l_reportmgr.prepareCurrencyRateReport(info);
				if (l_jrPrint.getPages().isEmpty())
					return HttpStatus.NOT_FOUND;
				l_session.setAttribute(SessionConstants.REPORT_DATA, l_jrPrint);
			} catch (Exception e) {
				e.printStackTrace();
				return HttpStatus.INTERNAL_SERVER_ERROR;
			}
			return HttpStatus.OK;
		}

	public int prepareCurrencyReport(CurrencyRpt info) {
		try {
			HttpSession l_session = getSession();
			CurrencyMgr l_reportmgr = new CurrencyMgr(getSession().getId());
			JasperPrint l_jrPrint = l_reportmgr.prepareCurrencyReport(info);
			if (l_jrPrint.getPages().isEmpty())
				return HttpStatus.NOT_FOUND;
			l_session.setAttribute(SessionConstants.REPORT_DATA, l_jrPrint);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}
	
	// Country

	public int createCountry(Country country) {
		try {
			CountryMgr l_CountryMgr = new CountryMgr(getSession().getId());
			country.setUserid(Long.parseLong(getSession().getAttribute(
					SessionConstants.USER_ID).toString()));
			l_CountryMgr.create(country);
		} catch (Exception e) {
			if (e instanceof DataDuplicationException)
				return HttpStatus.BAD_REQUEST;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	public int updateCountry(Country country) {
		try {
			CountryMgr l_CountryMgr = new CountryMgr(getSession().getId());
			country.setUserid(Long.parseLong(getSession().getAttribute(
					SessionConstants.USER_ID).toString()));
			l_CountryMgr.update(country);
		} catch (Exception e) {
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	public int deleteCountry(long countryId) {
		try {
			CountryMgr l_CountryMgr = new CountryMgr(getSession().getId());
			l_CountryMgr.delete(countryId,Long.parseLong(getSession().getAttribute(
					SessionConstants.USER_ID).toString()));
			l_CountryMgr.delete(countryId,Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
		} catch (Exception e) {
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	public CountryDataListDTO findCountry(Country cri) {
		CountryDataListDTO l_dto = new CountryDataListDTO();
		try {
			CountryMgr l_CountryMgr = new CountryMgr(getSession().getId());
			l_dto.setDataList(l_CountryMgr.findCountry(cri));
		} catch (Exception e) {
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	public int prepareCountryReport(CountryRpt info) {
		try {
			HttpSession l_session = getSession();
			CountryMgr l_reportmgr = new CountryMgr(l_session.getId());
			JasperPrint l_jrPrint = l_reportmgr.prepareCountryReport(info);
			if (l_jrPrint.getPages().isEmpty())
				return HttpStatus.NOT_FOUND;
			l_session.setAttribute(SessionConstants.REPORT_DATA, l_jrPrint);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	public Country findCountryByCode(Country entity) {
		try {
			CountryMgr l_CountryMgr = new CountryMgr(getSession().getId());
			return l_CountryMgr.findCountryByCode(entity);
		} catch (Exception e) {
			return null;
		}
		
	}
	// End Country

	// BranchType
	public int createBranchType(BranchType branchType) {
		try {
			BranchTypeMgr l_BranchTypeMgr = new BranchTypeMgr(getSession()
					.getId());
			branchType.setBranchID(Long.parseLong(getSession().getAttribute(
					SessionConstants.BRANCH_ID).toString()));
			branchType.setUserid(Long.parseLong(getSession().getAttribute(
					SessionConstants.USER_ID).toString()));
			l_BranchTypeMgr.create(branchType);
		} catch (Exception e) {
			if (e instanceof DataDuplicationException)
				return HttpStatus.BAD_REQUEST;
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	public int updateBranchType(BranchType branchType) {
		try {
			BranchTypeMgr l_BranchTypeMgr = new BranchTypeMgr(getSession()
					.getId());
			branchType.setBranchID(Long.parseLong(getSession().getAttribute(
					SessionConstants.BRANCH_ID).toString()));
			branchType.setUserid(Long.parseLong(getSession().getAttribute(
					SessionConstants.USER_ID).toString()));
			l_BranchTypeMgr.update(branchType);
		} catch (Exception e) {
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public int deleteBranchType(long bankId) {
		try {
			BranchTypeMgr l_BranchTypeMgr = new BranchTypeMgr(getSession()
					.getId());
			l_BranchTypeMgr.delete(bankId,Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
		} catch (Exception e) {
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	@Override
	public BranchTypeDataListDTO findBranchTypes(BranchType cri) {
		BranchTypeDataListDTO l_dto = new BranchTypeDataListDTO();
		try {
			cri.setBranchId(Long.parseLong(getSession().getAttribute(
					SessionConstants.BRANCH_ID).toString()));
			BranchTypeMgr l_BranchTypeMgr = new BranchTypeMgr(getSession()
					.getId());
			l_dto.setDataList(l_BranchTypeMgr.findBranchTypes(cri));
		} catch (Exception e) {
			l_dto.setHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		return l_dto;
	}

	@Override
	public BranchType findBranchTypeByCode(BranchType entity) {
		try {
			BranchTypeMgr l_BranchTypeMgr = new BranchTypeMgr(getSession()
					.getId());
			return l_BranchTypeMgr.findBranchTypeByCode(entity);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public int prepareBranchTypeReport(BranchTypeRpt branchType) {
		try {
			HttpSession l_session = getSession();
			BranchTypeMgr l_reportmgr = new BranchTypeMgr(l_session.getId());
			branchType.setBranchID(Long.parseLong(getSession().getAttribute(
					SessionConstants.BRANCH_ID).toString()));
			JasperPrint l_jrPrint = l_reportmgr
					.prepareBranchTypeReport(branchType);
			if (l_jrPrint.getPages().isEmpty())
				return HttpStatus.NOT_FOUND;
			l_session.setAttribute(SessionConstants.REPORT_DATA, l_jrPrint);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	// End BranchType

	// For Branch Report
	public int prepareBranchReport(BranchRpt info) {
		try {
			HttpSession l_session = getSession();
			BranchMgr l_reportmgr = new BranchMgr(l_session.getId());
			JasperPrint l_jrPrint = l_reportmgr.prepareBranchReport(info);
			if (l_jrPrint.getPages().isEmpty())
				return HttpStatus.NOT_FOUND;
			l_session.setAttribute(SessionConstants.REPORT_DATA, l_jrPrint);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpStatus.INTERNAL_SERVER_ERROR;
		}
		return HttpStatus.OK;
	}

	// End Branch Report
	@Override
	public CurrencyRate getSourcetoHomeRate(long source_currency) {
		CurrencyRate l_result = new CurrencyRate();
		try {
			CurrencyRateMgr l_CurrencyRateMgr = new CurrencyRateMgr(getSession().getId());
			l_result.setBranchId(Long.parseLong(getSession().getAttribute(
					SessionConstants.BRANCH_ID).toString()));
			l_result = l_CurrencyRateMgr.getSourcetoHomeRate(source_currency,l_result.getBranchId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return l_result;
	}

	@Override
	public int heartBeat(Date latestLoginDateTime) {
		try {
			SystemMgr l_mgr = new SystemMgr(getSession().getId());
			l_mgr.updateLatestLoginDateTime(latestLoginDateTime, Long.parseLong(getSession().getAttribute(SessionConstants.USER_ID).toString()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return HttpStatus.OK;
	}

	@Override
	public SystemProperty getSystemProperty(String key) {
		try {
			SystemMgr l_mgr = new SystemMgr(getSession().getId());
			return l_mgr.getSystemProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
